/**
 * Created by tronke on 2017/8/23.
 */

//name=value;[expires=date];[path=路径];[domain=域名];[secure]
// key=value;expires=?;path=?;domain=?;secure
//secure 如果加了 secure 只能 https
/**
 * 设置操作
 * @param {string} key
 * @param {string} value
 * @param {Date} date
 * @param {string} path1
 * @param {string} domain
 * @param {string} secure
 */
function setCookie(key, value, date, path, domain, secure) {
    var str = "";
    if (key && value) {
        str += encodeURIComponent(key) + "=" + encodeURIComponent(value) + ";"
    }
    //判断你传进来的是否是一个 日期对象
    if (date instanceof Date) {
        str += "expires=" + date + ";";
    }
    //如果 path没有传值就是nudefined 就是false，是false就不会进入判断
    if (path) {
        str += "path=" + path + ";";
    }
    if (domain) {
        str += "domain=" + domain + ";";
    }
    if (secure == "secure") {
        str += "secure";
    }
    document.cookie = str;
}
//获取cookie操作
function getCookieAll() {
    var obj = {};
    /*
     * uanme=%E5%BC%A0%E8%8D%A3%E8%BF%AA; uanme1=%E5%BC%A0%E8%8D%A3%E8%BF%AA;  uanme2=%E5%BC%A0%E8%8D%A3%E8%BF%AA
     *
     * uanme=%E5%BC%A0%E8%8D%A3%E8%BF%AA
     * */
    var str = document.cookie;
    if (str.indexOf(";")!= -1) {
        //多条记录
        //uanme=%E5%BC%A0%E8%8D%A3%E8%BF%AA; uanme1=%E5%BC%A0%E8%8D%A3%E8%BF%AA;  uanme2=%E5%BC%A0%E8%8D%A3%E8%BF%AA
        var arr1 = str.split(';');//一个数组 [uanme=%E5%BC%A0%E8%8D%A3%E8%BF%AA,uanme1=%E5%BC%A0%E8%8D%A3%E8%BF%AA,uanme2=%E5%BC%A0%E8%8D%A3%E8%BF%AA]

        for (var i = 0; i < arr1.length; i++) {
            var arr2 = arr1[i].split('='); // uanme=%E5%BC%A0%E8%8D%A3%E8%BF%AA  ===> [uanme,%E5%BC%A0%E8%8D%A3%E8%BF%AA]
            obj[decodeURIComponent(arr2[0].trim())] = decodeURIComponent(arr2[1].trim());
        }
    } else {
        //只有1条   uanme=%E5%BC%A0%E8%8D%A3%E8%BF%AA
        // {uanme:"%E5%BC%A0%E8%8D%A3%E8%BF%AA"}

        if(str.indexOf('=')!=-1){
            var arr = str.split('=');
            obj[decodeURIComponent(arr[0].trim())] = decodeURIComponent(arr[1].trim());
        }
    }
    return obj;
}
//根据键获取值
function getCookieValueByKey(key) {
   var obj=getCookieAll(); // {zzzz:ccc,sdasdsalkh:dsfsadf}
    return obj[key];
}

//通过键取删除指定cookie  刘德华
function removeCookie(key) {
    var date=new Date();
        date.setDate(date.getDate()-10000);
    document.cookie=encodeURIComponent(key)+"= ;expires="+date;
}